package sg.edu.nus.iss.jass.gui;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.JPanel;

import sg.edu.nus.iss.jass.controller.JobSearchController;
import sg.edu.nus.iss.jass.controller.JobSearchController.JobAdFunction;
import sg.edu.nus.iss.jass.controller.MainController;
import sg.edu.nus.iss.jass.dto.JobAdvert;
import sg.edu.nus.iss.jass.gui.component.CustomizableJTable;
import sg.edu.nus.iss.jass.gui.component.ScrollableCustomizableJTable;
import sg.edu.nus.iss.jass.privilege.AdvertiserRole;
import sg.edu.nus.iss.jass.privilege.Role;
import sg.edu.nus.iss.jass.privilege.SessionContext;
import sg.edu.nus.iss.jass.util.Util;

public class SearchJobResultPanel extends JPanel {

	private JobSearchFormPanel parent;
	private JobSearchController jobSearchController;
	private CustomizableJTable jobAdTable;
	private ScrollableCustomizableJTable scrollableJTable;
	
	private List<JobAdvert> jobAdverts;
	
	private Role role;
	
	public SearchJobResultPanel(JobSearchFormPanel parent){
		super();
		this.parent = parent;
		jobSearchController = MainController.getInstance().getJobSearchController();
		role = SessionContext.getUser().getRole();
		this.createSearchResultPanel();
	}

	private void createSearchResultPanel() {
		{
			this.setLayout(null);
			Vector<String> columnNames = new Vector<String>();
			if(role instanceof AdvertiserRole){
				 columnNames.add("Select");
	        } else {
	        }
	       
	        columnNames.add("Job Title");
	        columnNames.add("Company");
	        columnNames.add("Salary Range");
	        columnNames.add("Start Date");
	        columnNames.add("View");
	        if(role instanceof AdvertiserRole){
	        	columnNames.add("Modify");
	        } else {
	        }
	        
	        
	        String [] styles = null;
	        
	        
	        int[] columnWidth = null;
	        if(role instanceof AdvertiserRole){
	        	styles = new String[]{CustomizableJTable.CHECKBOX_STYLE,
	   	         CustomizableJTable.NORMAL_STYLE,
	   	         CustomizableJTable.NORMAL_STYLE,
	   	         CustomizableJTable.NORMAL_STYLE,
	   	         CustomizableJTable.NORMAL_STYLE,
	   	         CustomizableJTable.BUTTON_STYLE,
	   	         CustomizableJTable.BUTTON_STYLE
	   	        };
	        	columnWidth = new int[]{100,200,200,300,300,140,140};
	        } else {
	        	styles = new String[]{
	   	   	         CustomizableJTable.NORMAL_STYLE,
	   	   	         CustomizableJTable.NORMAL_STYLE,
	   	   	         CustomizableJTable.NORMAL_STYLE,
	   	   	         CustomizableJTable.NORMAL_STYLE,
	   	   	         CustomizableJTable.BUTTON_STYLE
	   	   	        };
	        	columnWidth = new int[]{200,200,300,300,140};
	        }
			
			try {
				jobAdTable = new CustomizableJTable(columnNames, 0, styles, columnWidth){
					public void doButtonDownAction(int xcellpos, int ycellpos) {
						if(role instanceof AdvertiserRole){
							if(ycellpos == 5){
								jobSearchController.processJobAd(getSelectedJob(xcellpos), JobAdFunction.view);
							}else if(ycellpos == 6){
								jobSearchController.processJobAd(getSelectedJob(xcellpos), JobAdFunction.modify);
							}
						} else {
							if(ycellpos == 4){
								jobSearchController.processJobAd(getSelectedJob(xcellpos), JobAdFunction.view);
							}
						}
					}
				};
			} catch (Exception e) {
				e.printStackTrace();
			}
			scrollableJTable = new ScrollableCustomizableJTable(jobAdTable);
			scrollableJTable.setSize(700, 400);
		}
		this.add(scrollableJTable);
	}
	
	protected List<JobAdvert> getSelectedJob(int xcellpos) {
		List<JobAdvert> jobAd = new ArrayList<JobAdvert>();
		jobAd.add(this.jobAdverts.get(xcellpos));
		return jobAd;
	}

	public void refreshSearchResult(List<JobAdvert> jobAdverts){
		this.jobAdverts = jobAdverts;
		jobAdTable.refreshTable(this.jobAdverts.size());
		this.jobAdvert_BindTable();
	}
	
	private void jobAdvert_BindTable() {
	    for (int i=0; i < this.jobAdverts.size(); i++) {
	        JobAdvert jobAd = jobAdverts.get(i);
	        if(role instanceof AdvertiserRole){
	        	jobAdTable.setValueAt(new Boolean(false), i, 0);
	        	jobAdTable.setValueAt(jobAd.getTitle(), i, 1);
		        jobAdTable.setValueAt(jobAd.getCompany().getName(), i, 2);
		        jobAdTable.setValueAt(jobAd.getMinSalary() + "-" + jobAd.getMaxSalary(), i, 3);
		        jobAdTable.setValueAt(Util.getStringFromdate(jobAd.getStartDate()), i, 4);
		        jobAdTable.setValueAt("View", i, 5);
	        	jobAdTable.setValueAt("Modify", i, 6);
	        } else {
	        	jobAdTable.setValueAt(jobAd.getTitle(), i, 0);
		        jobAdTable.setValueAt(jobAd.getCompany().getName(), i, 1);
		        jobAdTable.setValueAt(jobAd.getMinSalary() + "-" + jobAd.getMaxSalary(), i, 2);
		        jobAdTable.setValueAt(Util.getStringFromdate(jobAd.getStartDate()), i, 3);
		        jobAdTable.setValueAt("View", i, 4);
	        }
	    }
	}
	
	public List<JobAdvert> getSelectedJob(){
		List<JobAdvert> jobAds = new ArrayList<JobAdvert>();
		for(int i=0; i < jobAdTable.getRowCount(); i++){
			Boolean aBool = (Boolean)jobAdTable.getValueAt(i, 0);
			if(aBool){
				jobAds.add(jobAdverts.get(i));
			}
		}
		return jobAds;
	}
	
	public void selectJobAd(Boolean select){
		for(int i=0; i < jobAdTable.getRowCount(); i++)
			jobAdTable.setValueAt(select, i, 0);
	}
	
	public void clearTable(){
		jobAdTable.removeData();
		jobAdTable.repaint();
	}
}
